package com.example.mediacenterufrj;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.os.Build;

public class MenuActivity extends Activity {
	
	private void CopyRAWtoSDCard(int id, String path) throws IOException {
	    InputStream in = getResources().openRawResource(id);
	    FileOutputStream out = new FileOutputStream(path);
	    byte[] buff = new byte[1024];
	    int read = 0;
	    try {
	        while ((read = in.read(buff)) > 0) {
	            out.write(buff, 0, read);
	        }
	    } finally {
	        in.close();
	        out.close();
	    }
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		
		String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();
		//String rootPath = "/mnt/shared/AndroidShared";
		
		File media = new File(rootPath + "/MediaUFRJ");
		File videos = new File(rootPath + "/MediaUFRJ/videos");
		File fotos = new File(rootPath + "/MediaUFRJ/photos");
		
		media.mkdir();
		videos.mkdir();
		fotos.mkdirs();
		
		try {
			CopyRAWtoSDCard(R.raw.wallpaper, media.getAbsolutePath() + "/wallpaper.jpg");
			CopyRAWtoSDCard(R.raw.text, media.getAbsolutePath() + "/shareText.txt");
			
			CopyRAWtoSDCard(R.raw.img1, fotos.getAbsolutePath() + "/img1.jpg");
			CopyRAWtoSDCard(R.raw.img2, fotos.getAbsolutePath() + "/img2.jpg");
			CopyRAWtoSDCard(R.raw.img3, fotos.getAbsolutePath() + "/img3.jpg");
			CopyRAWtoSDCard(R.raw.img4, fotos.getAbsolutePath() + "/img4.jpg");
			CopyRAWtoSDCard(R.raw.img5, fotos.getAbsolutePath() + "/img5.jpg");
			CopyRAWtoSDCard(R.raw.img6, fotos.getAbsolutePath() + "/img6.jpg");
			CopyRAWtoSDCard(R.raw.img7, fotos.getAbsolutePath() + "/img7.jpg");
			CopyRAWtoSDCard(R.raw.img8, fotos.getAbsolutePath() + "/img8.jpg");
			
			CopyRAWtoSDCard(R.raw.documentariesandyou1, videos.getAbsolutePath() + "/documentariesandyou1.mp4");
			CopyRAWtoSDCard(R.raw.documentariesandyou1, videos.getAbsolutePath() + "/documentariesandyou2.mp4");
			CopyRAWtoSDCard(R.raw.documentariesandyou1, videos.getAbsolutePath() + "/documentariesandyou3.mp4");
			CopyRAWtoSDCard(R.raw.documentariesandyou1, videos.getAbsolutePath() + "/documentariesandyou4.mp4");
			CopyRAWtoSDCard(R.raw.documentariesandyou1, videos.getAbsolutePath() + "/documentariesandyou5.mp4");
			CopyRAWtoSDCard(R.raw.documentariesandyou1, videos.getAbsolutePath() + "/documentariesandyou6.mp4");
			CopyRAWtoSDCard(R.raw.documentariesandyou1, videos.getAbsolutePath() + "/documentariesandyou7.mp4");
			CopyRAWtoSDCard(R.raw.documentariesandyou1, videos.getAbsolutePath() + "/documentariesandyou8.mp4");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		final String imageFilesContainterPath = rootPath+ "/MediaUFRJ/photos";
		final String videoFilesContainterPath = rootPath + "/MediaUFRJ/videos";
		
		String wallpaperPath = rootPath + "/MediaUFRJ/wallpaper.jpg";
		String shareTextPath = rootPath + "/MediaUFRJ/shareText.txt";
		
		if(Resources.listaImagens.size() == 0) {
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					Resources.populateImages(imageFilesContainterPath);
				}
			}).run();			
		}
		
		if(Resources.listaVideoThumbnails.size() == 0) {
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					Resources.populateVideos(videoFilesContainterPath);
				}
			}).run();	
		}
		
		if(Resources.wallpaper == null)  {
			Resources.loadWallpaper(wallpaperPath);
		}
		
		if(Resources.shareText.equalsIgnoreCase("")) {
			Resources.loadShareText(shareTextPath);
		}

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {

			Intent sendIntent = new Intent();
			sendIntent.setAction(Intent.ACTION_SEND);
			sendIntent.putExtra(Intent.EXTRA_TEXT, Resources.shareText);
			sendIntent.setType("text/plain");
			startActivity(sendIntent);

			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_menu, container,
					false);
			
			
			ImageView wallpaper = (ImageView) rootView.findViewById(R.id.imgWallapper);
			
			Bitmap myBitmap = Resources.wallpaper;
			
			if(myBitmap != null) {
				DisplayMetrics metrics = rootView.getContext().getResources().getDisplayMetrics();
				int width = metrics.widthPixels;
				int height = metrics.heightPixels - 48 - 33; // - barra de menu - barras de sistema
				
				int widthImg, heightImg;
				
				Bitmap resizedbitmap1;
				
				if((float)width/height > (float)myBitmap.getWidth()/myBitmap.getHeight()) {
					widthImg = myBitmap.getWidth();
					heightImg = (int)(((float)height*myBitmap.getWidth())/width); 				
					
					resizedbitmap1 = Bitmap.createBitmap(myBitmap, 0, (myBitmap.getHeight() - heightImg) / 2, widthImg, heightImg);
					
					wallpaper.setImageBitmap(resizedbitmap1);
				} else {
					heightImg = myBitmap.getHeight();
					widthImg = (int)(((float)width*myBitmap.getHeight())/height); 				
					
					resizedbitmap1 = Bitmap.createBitmap(myBitmap, (myBitmap.getWidth() - widthImg) / 2, 0, widthImg, heightImg);
					
					wallpaper.setImageBitmap(resizedbitmap1);
					
				}
				
				
				wallpaper.setVisibility(1);
			}
					
			
			//
			final View view = rootView;
			Button botaoAlbum = (Button) rootView.findViewById(R.id.buttonIniciaAlbum);
			botaoAlbum.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(view.getContext(), MainActivity.class);
					
					startActivity(intent);
					
				}
			});
			
			return rootView;
		}
	}

}
